package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.fliter.PhotoProcessing;
import com.stark.imgedit.model.ColorTuneRange;
import com.stark.imgedit.utils.BmpUtil;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.adapter.PicCutAdapter;
import flc.ast.adapter.PictureFilterAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.bean.MyCutBean;
import flc.ast.bean.MyPictureFilterBean;
import flc.ast.databinding.ActivityPicEditBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.view.container.StkLinearLayout;

public class PicEditActivity extends BaseAc<ActivityPicEditBinding> {

    public static String imgPath;
    private Bitmap mImgBitmap, saveBitmap;
    private PicCutAdapter cutAdapter;
    private PictureFilterAdapter filterAdapter;
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private float myBrightness = 1;//亮度
    private float myContrast = 0;//对比度
    private float mySaturation = 1;//饱和度
    private ColorAdapter colorAdapter, textColorAdapter;
    private int cutPos = 0, filterPos = 0, colorPos = 0, textColorPos = 0;
    private Dialog mySaveDialog;
    private SeekBar seekBar;
    private TextView textView;
    private StkLinearLayout llPicSaveSuc;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_edit;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicEditBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mDataBinding.rvPicCutList.setLayoutManager(new GridLayoutManager(mContext, 4));
        cutAdapter = new PicCutAdapter();
        mDataBinding.rvPicCutList.setAdapter(cutAdapter);
        cutAdapter.setOnItemClickListener(this);

        mDataBinding.rvPicFilterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new PictureFilterAdapter();
        mDataBinding.rvPicFilterList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);

        mDataBinding.rvPicDrawColorList.setLayoutManager(new GridLayoutManager(mContext, 8));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvPicDrawColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);

        mDataBinding.rvPicTextColorList.setLayoutManager(new GridLayoutManager(mContext, 8));
        textColorAdapter = new ColorAdapter();
        mDataBinding.rvPicTextColorList.setAdapter(textColorAdapter);
        textColorAdapter.setOnItemClickListener(this);

        mDataBinding.ivPicCut.setOnClickListener(this);
        mDataBinding.ivCutCancel.setOnClickListener(this);
        mDataBinding.ivCutConfirm.setOnClickListener(this);
        mDataBinding.ivPicFilter.setOnClickListener(this);
        mDataBinding.ivFilterCancel.setOnClickListener(this);
        mDataBinding.ivFilterConfirm.setOnClickListener(this);
        mDataBinding.ivPicDraw.setOnClickListener(this);
        mDataBinding.ivDrawCancel.setOnClickListener(this);
        mDataBinding.ivDrawConfirm.setOnClickListener(this);
        mDataBinding.ivDrawBrush.setOnClickListener(this);
        mDataBinding.ivDrawEraser.setOnClickListener(this);
        mDataBinding.ivPicText.setOnClickListener(this);
        mDataBinding.ivTextCancel.setOnClickListener(this);
        mDataBinding.ivTextConfirm.setOnClickListener(this);
        mDataBinding.ivTextAdd.setOnClickListener(this);
        mDataBinding.ivPicAdjust.setOnClickListener(this);
        mDataBinding.ivAdjustCancel.setOnClickListener(this);
        mDataBinding.ivAdjustConfirm.setOnClickListener(this);
        mDataBinding.tvBrightness.setOnClickListener(this);
        mDataBinding.tvContrast.setOnClickListener(this);
        mDataBinding.tvSaturation.setOnClickListener(this);
        mDataBinding.ivPicEditSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPicCut://裁剪
                mDataBinding.llPicEditBottom.setVisibility(View.GONE);
                mDataBinding.llPicCut.setVisibility(View.VISIBLE);
                mDataBinding.cropImgView.setVisibility(View.VISIBLE);
                break;
            case R.id.ivCutConfirm:
                cutImg();
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicCut.setVisibility(View.GONE);
                mDataBinding.cropImgView.setVisibility(View.GONE);
                break;
            case R.id.ivCutCancel:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicCut.setVisibility(View.GONE);
                mDataBinding.cropImgView.setVisibility(View.GONE);
                break;
            case R.id.ivPicFilter://滤镜
                mDataBinding.llPicEditBottom.setVisibility(View.GONE);
                mDataBinding.llPicFilter.setVisibility(View.VISIBLE);
                break;
            case R.id.ivFilterConfirm:
                mImgBitmap = saveBitmap;
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicFilter.setVisibility(View.GONE);
                break;
            case R.id.ivFilterCancel:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicFilter.setVisibility(View.GONE);
                saveBitmap = mImgBitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
                break;
            case R.id.ivPicDraw://画笔
                mDataBinding.llPicEditBottom.setVisibility(View.GONE);
                mDataBinding.llPicDraw.setVisibility(View.VISIBLE);
                mDataBinding.myDrawingView.setVisibility(View.VISIBLE);
                break;
            case R.id.ivDrawConfirm:
                setDraw();
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicDraw.setVisibility(View.GONE);
                mDataBinding.myDrawingView.setVisibility(View.GONE);
                break;
            case R.id.ivDrawCancel:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicDraw.setVisibility(View.GONE);
                mDataBinding.myDrawingView.setVisibility(View.GONE);
                mDataBinding.myDrawingView.clear();
                saveBitmap = mImgBitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
                break;
            case R.id.ivDrawBrush://画笔
                clearView();
                mDataBinding.ivDrawBrush.setImageResource(R.drawable.ahuabxz);
                mDataBinding.sbBrush.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(false);
                penBrush.setSize(paintWidth);
                break;
            case R.id.ivDrawEraser://橡皮擦
                clearView();
                mDataBinding.ivDrawEraser.setImageResource(R.drawable.axiangvpcxz);
                mDataBinding.sbEraser.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(true);
                penBrush.setSize(eraserWidth);
                break;
            case R.id.ivPicText://文字
                mDataBinding.txtStickerView.setShowHelpBox(false);
                mDataBinding.llPicEditBottom.setVisibility(View.GONE);
                mDataBinding.llPicText.setVisibility(View.VISIBLE);
                break;
            case R.id.ivTextConfirm:
                setText();
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicText.setVisibility(View.GONE);
                break;
            case R.id.ivTextCancel:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicText.setVisibility(View.GONE);
                mDataBinding.txtStickerView.clearTextContent();
                mDataBinding.txtStickerView.resetView();
                break;
            case R.id.ivTextAdd://添加文字
                KeyboardUtils.hideSoftInput(this);
                if (TextUtils.isEmpty(mDataBinding.etTextContent.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_text);
                    return;
                }
                mDataBinding.txtStickerView.setText(mDataBinding.etTextContent.getText().toString());
                break;
            case R.id.ivPicAdjust://调节
                mDataBinding.llPicEditBottom.setVisibility(View.GONE);
                mDataBinding.llPicAdjust.setVisibility(View.VISIBLE);
                break;
            case R.id.ivAdjustConfirm:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicAdjust.setVisibility(View.GONE);
                mImgBitmap = saveBitmap;
                break;
            case R.id.ivAdjustCancel:
                mDataBinding.llPicEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llPicAdjust.setVisibility(View.GONE);
                saveBitmap = mImgBitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
                break;
            case R.id.tvBrightness://亮度
                clearAdjustView();
                mDataBinding.tvBrightness.setTextSize(18);
                mDataBinding.tvBrightness.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivBrightnessSel.setVisibility(View.VISIBLE);
                mDataBinding.llBrightness.setVisibility(View.VISIBLE);
                break;
            case R.id.tvContrast://对比度
                clearAdjustView();
                mDataBinding.tvContrast.setTextSize(18);
                mDataBinding.tvContrast.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivContrastSel.setVisibility(View.VISIBLE);
                mDataBinding.llContrast.setVisibility(View.VISIBLE);
                break;
            case R.id.tvSaturation://对比度
                clearAdjustView();
                mDataBinding.tvSaturation.setTextSize(18);
                mDataBinding.tvSaturation.setTextColor(Color.parseColor("#000000"));
                mDataBinding.ivSaturationSel.setVisibility(View.VISIBLE);
                mDataBinding.llSaturation.setVisibility(View.VISIBLE);
                break;
            case R.id.ivPicEditSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == cutAdapter) {
            cutAdapter.getItem(cutPos).setSelect(false);
            cutAdapter.notifyItemChanged(cutPos);
            cutPos = position;
            cutAdapter.getItem(position).setSelect(true);
            cutAdapter.notifyItemChanged(position);
            mDataBinding.cropImgView.setRatioCropRect(mDataBinding.imageViewTouch.getBitmapRect(),
                    cutAdapter.getItem(position).getRatio());
        } else if (adapter == filterAdapter) {
            filterAdapter.getItem(filterPos).setSelect(false);
            filterAdapter.notifyItemChanged(filterPos);
            filterPos = position;
            filterAdapter.getItem(position).setSelect(true);
            filterAdapter.notifyItemChanged(position);
            setFilter(position);
        } else if (adapter == colorAdapter) {
            colorAdapter.getItem(colorPos).setSelect(false);
            colorAdapter.notifyItemChanged(colorPos);
            colorPos = position;
            colorAdapter.getItem(position).setSelect(true);
            colorAdapter.notifyItemChanged(position);
            penBrush.setColor(Color.parseColor(colorAdapter.getItem(position).getColor()));//默认画笔颜色
        } else if (adapter == textColorAdapter) {
            textColorAdapter.getItem(textColorPos).setSelect(false);
            textColorAdapter.notifyItemChanged(textColorPos);
            textColorPos = position;
            textColorAdapter.getItem(position).setSelect(true);
            textColorAdapter.notifyItemChanged(position);
            mDataBinding.txtStickerView.setTextColor(Color.parseColor(textColorAdapter.getItem(position).getColor()));
        }
    }

    @Override
    protected void initData() {
        //设置图片
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        saveBitmap = mImgBitmap;
        mDataBinding.imageViewTouch.setImageBitmap(mImgBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        mDataBinding.imageViewTouch.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.imageViewTouch.getBitmapRect();
                mDataBinding.cropImgView.setCropRect(bitmapRect);
            }
        });
        //裁剪数据
        List<MyCutBean> listCut = new ArrayList<>();
        listCut.add(new MyCutBean(getString(R.string.pic_cut_text), R.drawable.aziycj, -1f, true));
        listCut.add(new MyCutBean("1:1", R.drawable.a1_1, 1f, false));
        listCut.add(new MyCutBean("4:3", R.drawable.a4_3, 4f / 3f, false));
        listCut.add(new MyCutBean("9:16", R.drawable.a9_16, 9f / 16f, false));
        cutAdapter.setList(listCut);
        //滤镜数据
        String[] mFilters = getResources().getStringArray(R.array.filters);
        List<MyPictureFilterBean> listFilter = new ArrayList<>();
        listFilter.add(new MyPictureFilterBean(mFilters[0], "#00000000", true));
        listFilter.add(new MyPictureFilterBean(mFilters[1], "#323333", false));
        listFilter.add(new MyPictureFilterBean(mFilters[2], "#C376F3", false));
        listFilter.add(new MyPictureFilterBean(mFilters[3], "#6F7171", false));
        listFilter.add(new MyPictureFilterBean(mFilters[4], "#505151", false));
        listFilter.add(new MyPictureFilterBean(mFilters[5], "#999999", false));
        listFilter.add(new MyPictureFilterBean(mFilters[6], "#513232", false));
        listFilter.add(new MyPictureFilterBean(mFilters[7], "#787A7A", false));
        listFilter.add(new MyPictureFilterBean(mFilters[8], "#5000FF00", false));
        listFilter.add(new MyPictureFilterBean(mFilters[9], "#500000FF", false));
        listFilter.add(new MyPictureFilterBean(mFilters[10], "#50FF0000", false));
        listFilter.add(new MyPictureFilterBean(mFilters[11], "#50FFFF00", false));
        filterAdapter.setList(listFilter);
        //颜色数据
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#000000", true));
        listColor.add(new MyColorBean("#FFFFFF", false));
        listColor.add(new MyColorBean("#E12D2D", false));
        listColor.add(new MyColorBean("#FFA8A8", false));
        listColor.add(new MyColorBean("#FFB652", false));
        listColor.add(new MyColorBean("#FFF62C", false));
        listColor.add(new MyColorBean("#79BA56", false));
        listColor.add(new MyColorBean("#35B9B0", false));
        listColor.add(new MyColorBean("#3F68BC", false));
        listColor.add(new MyColorBean("#9F45F1", false));
        listColor.add(new MyColorBean("#E85C90", false));
        listColor.add(new MyColorBean("#C0C0C0", false));
        listColor.add(new MyColorBean("#CA58C9", false));
        listColor.add(new MyColorBean("#8A72E1", false));
        listColor.add(new MyColorBean("#46D5AF", false));
        listColor.add(new MyColorBean("#5E8C4F", false));
        colorAdapter.setList(listColor);
        textColorAdapter.setList(listColor);
        mDataBinding.txtStickerView.setTextColor(Color.parseColor(textColorAdapter.getItem(0).getColor()));
        initPen();
        //图片调节
        //初始化滑动控件
        initSb();
        //调节图片
        adjustImg();
        //保存弹框
        saveDialog();
    }

    //保存图片
    private void saveImg() {
        seekBar.setVisibility(View.VISIBLE);
        llPicSaveSuc.setVisibility(View.GONE);
        textView.setText(getString(R.string.pic_save_ing));
        mySaveDialog.show();
        seekBar.setProgress(new Random().nextInt(60));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        File file = ImageUtils.save2Album(saveBitmap, Bitmap.CompressFormat.PNG);
                        String path = PathUtils.getFilesPathExternalFirst() + Content.myImage + "/" + FileUtils.getFileName(file.getPath());
                        FileUtils.copy(file.getPath(), path);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        textView.setText(getString(R.string.pic_save_suc));
                        seekBar.setVisibility(View.GONE);
                        llPicSaveSuc.setVisibility(View.VISIBLE);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startActivity(HomeActivity.class);
                            }
                        }, 1000);
                    }
                });
            }
        }, 1000);
    }


    //裁剪图片
    private void cutImg() {
        showDialog(getString(R.string.pic_cut_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.imageViewTouch.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                saveBitmap = resultBit;
                mImgBitmap = saveBitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
            }
        });
    }

    //设置滤镜
    private void setFilter(final int f) {
        showDialog(getString(R.string.set_filter_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap retBitmap = Bitmap.createBitmap(mImgBitmap.copy(
                        Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.filterPhoto(retBitmap, f);
                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) return;
                saveBitmap = bitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
            }
        });
    }

    //设置画笔图片
    private void setDraw() {
        showDialog(getString(R.string.set_draw_ing));
//        RxUtil.create(new RxUtil.Callback<String>() {
//
//            @Override
//            public void doBackground(ObservableEmitter<String> emitter) {
        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlPicEditSave);
        saveBitmap = bitmap;
        mImgBitmap = saveBitmap;
        mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
        mDataBinding.myDrawingView.clear();
//                emitter.onNext("");
//            }
//
//            @Override
//            public void accept(String path) {
        dismissDialog();
//            }
//        });
    }

    public void setText() {
        showDialog(getString(R.string.set_draw_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Matrix touchMatrix = mDataBinding.imageViewTouch.getImageViewMatrix();
                Bitmap retBitmap = Bitmap.createBitmap(saveBitmap).copy(
                        Bitmap.Config.ARGB_8888, true);
                Canvas canvas = new Canvas(retBitmap);

                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());

                float[] f = new float[9];
                m.getValues(f);
                int dx = (int) f[Matrix.MTRANS_X];
                int dy = (int) f[Matrix.MTRANS_Y];
                float scale_x = f[Matrix.MSCALE_X];
                float scale_y = f[Matrix.MSCALE_Y];
                canvas.save();
                canvas.translate(dx, dy);
                canvas.scale(scale_x, scale_y);
                mDataBinding.txtStickerView.drawText(canvas, mDataBinding.txtStickerView.layout_x,
                        mDataBinding.txtStickerView.layout_y, mDataBinding.txtStickerView.mScale, mDataBinding.txtStickerView.mRotateAngle);
                canvas.restore();
                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                mDataBinding.txtStickerView.clearTextContent();
                mDataBinding.txtStickerView.resetView();
                saveBitmap = bitmap;
                mImgBitmap = saveBitmap;
                mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
            }
        });
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#000000"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小
        mDataBinding.sbBrush.setMax(50);
        mDataBinding.sbBrush.setProgress(5);
        mDataBinding.sbBrush.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //设置橡皮擦大小
        mDataBinding.sbEraser.setMax(50);
        mDataBinding.sbEraser.setProgress(5);
        mDataBinding.sbEraser.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //初始化控件
    private void initSb() {
        myBrightness = 1;
        mySaturation = 1;
        myContrast = 0;
        mDataBinding.sbBrightness.setProgress(calSeekBarProgress(mDataBinding.sbBrightness,
                myBrightness, ColorTuneRange.BRIGHTNESS));
        mDataBinding.sbSaturation.setProgress(calSeekBarProgress(mDataBinding.sbSaturation,
                mySaturation, ColorTuneRange.SATURATION));
        mDataBinding.sbContrast.setProgress(calSeekBarProgress(mDataBinding.sbContrast,
                myContrast, ColorTuneRange.CONTRAST));
        mDataBinding.tvBrightnessNum.setText(mDataBinding.sbBrightness.getProgress() + getString(R.string.unit_percent));
        mDataBinding.tvContrastNum.setText(mDataBinding.sbContrast.getProgress() + getString(R.string.unit_percent));
        mDataBinding.tvSaturationNum.setText(mDataBinding.sbSaturation.getProgress() + getString(R.string.unit_percent));
    }

    //调节图片
    private void adjustImg() {
        //brightness
        mDataBinding.sbBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                myBrightness = calSeekBarRealValue(mDataBinding.sbBrightness, progress, ColorTuneRange.BRIGHTNESS);
                //0-2
                float number = myBrightness * 100;
                mDataBinding.tvBrightnessNum.setText(mDataBinding.sbBrightness.getProgress() + getString(R.string.unit_percent));
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //saturation
        mDataBinding.sbSaturation.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mySaturation = calSeekBarRealValue(mDataBinding.sbSaturation, progress, ColorTuneRange.SATURATION);
                //0-2
                float number = mySaturation * 100;
                mDataBinding.tvSaturationNum.setText(mDataBinding.sbSaturation.getProgress() + getString(R.string.unit_percent));
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //contrast
        mDataBinding.sbContrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                myContrast = calSeekBarRealValue(mDataBinding.sbContrast, progress, ColorTuneRange.CONTRAST);
                //0-360
                float number = myContrast * 10 / 36;
                mDataBinding.tvContrastNum.setText(mDataBinding.sbContrast.getProgress() + getString(R.string.unit_percent));
                updateImageViewBitmap();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //改变图片显示
    private void updateImageViewBitmap() {
        Bitmap preRetBitmap = saveBitmap;
        saveBitmap = BmpUtil.adjust(mImgBitmap, myBrightness, mySaturation, myContrast);
        if (saveBitmap != null) {
            mDataBinding.imageViewTouch.setImageBitmap(saveBitmap);
            if (preRetBitmap != null && !preRetBitmap.isRecycled() && preRetBitmap != mImgBitmap) {
                preRetBitmap.recycle();
            }
        }
    }

    private float calSeekBarRealValue(SeekBar seekBar, int progress, ColorTuneRange range) {
        int progressRange = seekBar.getMax();
        float value = progress * 1.0f / progressRange * range.getRange() + range.getMin();
        return value;
    }

    private int calSeekBarProgress(SeekBar seekBar, float value, ColorTuneRange range) {
        float valuePercent = (value - range.getMin()) / range.getRange();
        int progress = (int) (seekBar.getMax() * valuePercent);
        return progress;
    }

    //管理控件
    private void clearView() {
        mDataBinding.ivDrawBrush.setImageResource(R.drawable.ahuab);
        mDataBinding.ivDrawEraser.setImageResource(R.drawable.axpc);
        mDataBinding.sbBrush.setVisibility(View.GONE);
        mDataBinding.sbEraser.setVisibility(View.GONE);
    }

    //管理控件
    private void clearAdjustView() {
        mDataBinding.tvBrightness.setTextSize(15);
        mDataBinding.tvBrightness.setTextColor(Color.parseColor("#9E9E9E"));
        mDataBinding.ivBrightnessSel.setVisibility(View.GONE);
        mDataBinding.llBrightness.setVisibility(View.GONE);
        mDataBinding.tvContrast.setTextSize(15);
        mDataBinding.tvContrast.setTextColor(Color.parseColor("#9E9E9E"));
        mDataBinding.ivContrastSel.setVisibility(View.GONE);
        mDataBinding.llContrast.setVisibility(View.GONE);
        mDataBinding.tvSaturation.setTextSize(15);
        mDataBinding.tvSaturation.setTextColor(Color.parseColor("#9E9E9E"));
        mDataBinding.ivSaturationSel.setVisibility(View.GONE);
        mDataBinding.llSaturation.setVisibility(View.GONE);
    }

    //保存弹框
    private void saveDialog() {
        mySaveDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pic_save, null);
        mySaveDialog.setContentView(inflate);
        mySaveDialog.setCancelable(true);
        Window dialogWindow = mySaveDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        seekBar = inflate.findViewById(R.id.sbProgress);
        textView = inflate.findViewById(R.id.tvPicSaveTitle);
        llPicSaveSuc = inflate.findViewById(R.id.llPicSaveSuc);
    }

}